package com.bookmanger.server.mapper;

import com.bookmanger.pojo.Entity.IsbnEntity;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface IsbnMapper {
    @Insert("INSERT INTO isbn_tb(isbn, title, author, price, publisher, publisher_date, summary, type_id, stock, id_delete, img) " +
            "VALUES(#{isbn}, #{title}, #{author}, #{price}, #{publisher}, #{publisherDate}, #{summary}, #{typeId}, #{stock}, 0, #{img})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(IsbnEntity isbnEntity);

    @Update("UPDATE isbn_tb SET id_delete = 1 WHERE id = #{id}")
    int deleteById(Integer id);
    
    @Update("UPDATE book SET is_delete = 1 WHERE isbn_id = #{isbnId}")
    int deleteBooksByIsbnId(Integer isbnId);

    @Update("UPDATE isbn_tb SET isbn=#{isbn}, title=#{title}, author=#{author}, price=#{price}, publisher=#{publisher}, " +
            "publisher_date=#{publisherDate}, summary=#{summary}, type_id=#{typeId}, img=#{img} WHERE id=#{id}")
    int update(IsbnEntity isbnEntity);

    @Update("UPDATE isbn_tb SET stock = CASE WHEN stock + #{count} < 0 THEN -1 ELSE stock + #{count} END WHERE id = #{id}")
    int updateStock(@Param("id") Integer id, @Param("count") Integer count);

    @Select("SELECT * FROM isbn_tb WHERE id = #{id} AND id_delete = 0")
    IsbnEntity selectById(Integer id);

    @Select("SELECT * FROM isbn_tb WHERE id_delete = 0 AND " +
            "(title LIKE CONCAT('%', #{keyword}, '%') " +
            "OR author LIKE CONCAT('%', #{keyword}, '%') " +
            "OR isbn LIKE CONCAT('%', #{keyword}, '%') " +
            "OR publisher LIKE CONCAT('%', #{keyword}, '%'))")
    List<IsbnEntity> search(String keyword);

    @Select("SELECT * FROM isbn_tb WHERE id_delete = 0")
    List<IsbnEntity> findAll();
}